import { computed, watch, ref } from "vue";
import useMultipartOssStore from "@/store/modules/multipartOss";

export default (forms, modelValue) => {
  const multipartOssStore = useMultipartOssStore();

  const isUploadFile = ref(false);
  const isGetUploadFileUrl = ref(false);

  const fileCmps = ["ImageUpload", "FileUpload", "VideoUpload"];
  const fileCmpFileIds = computed(() => {
    return forms.value.filter(item => fileCmps.includes(item.name));
  });

  watch(
    () => modelValue.value,
    val => {
      if (fileCmpFileIds.value.length) {
        let files = [];
        fileCmpFileIds.value.forEach(item => {
          files = [...files, ...val[item.id]];
        });
        // 是否存在还在上传的文件
        isUploadFile.value = !!files.filter(
          item => multipartOssStore.loadingArr[item.fileId] !== void 0 && multipartOssStore.loadingArr[item.fileId] !== 100
        ).length;
        // 是否存在还未获取到url的文件
        isGetUploadFileUrl.value = !!files.filter(item => !multipartOssStore.completeState[item.fileId]).length;
      }
    },
    { deep: true }
  );

  return {
    isUploadFile,
    isGetUploadFileUrl
  };
};
